<?php
class SearchController extends AppController {
    
    public $components = array('RequestHandler','GCS','YCS','BCS');
    public $start = 0;
    public $limit = 10;
    public $type = 'web';
    public $provider = 'google';
    public $format = 'json';
    
    public function beforefilter(){
        $this->start = isset($_GET['s']) ?  $_GET['s'] : $this->start;
        $this->limit = isset($_GET['l']) ?  $_GET['l'] : $this->limit;
        $this->type = isset($_GET['t']) ?  $_GET['t'] : $this->type;
        $this->provider = isset($_GET['p']) ?  $_GET['p'] : $this->provider;
        $this->format = isset($_GET['f']) ?  $_GET['f'] : $this->format;
    }
    
    /******
     * Query = q, start = s, limit = l, format = f
     * type = t type means type of request like web, image, video, news.
     * provider = p provider means name of search engine
     *     /*
    CREATE TABLE IF NOT EXISTS `redirect_zones` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `site_id` int(11) NOT NULL,
  `keyword` varchar(50) NOT NULL,
  `url` varchar(200) NOT NULL,
  `tollfree` varchar(50) NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT '0',
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  PRIMARY KEY (`id`)
)
    ******/
    
    public function index() {
        $response = array();
        $qs = $_GET;
        if(isset($qs['q']) && $qs['q'] != ''){
            switch($this->provider){
                case 'google' :
                    $response = $this->google($qs['q']);
                    break;
                case 'yahoo' :
                    $response = $this->yahoo($qs['q']);
                    break;
                case 'bing' :
                    $response = $this->bing($qs['q']);
                    break;
            }
        }else{
            $response['error'] = 'Search term empty';
        }
        $this->response($response);
    }
    
    private function google($query){
        return $this->GCS->search($query,$this->type, $this->start,$this->limit);
    }
    
    private function yahoo($query){
        return $this->YCS->search($query,$this->type, $this->start,$this->limit);
    }
    
    private function bing($query){
        return $this->BCS->search($query,$this->type, $this->start,$this->limit);
    }
    
    
    function response($data){
        header('Access-Control-Allow-Origin: *');
        switch($this->format){
            case 'json' :
                print json_encode($data);
                break;
            case 'xml' :
                print $this->array2xml($data);
                break;
        }
        exit;
    }
    
    function array2xml($array, $xml = false){
        if($xml === false){
            $xml = new SimpleXMLElement('<result/>');
        }
        foreach($array as $key => $value){
            if(is_array($value)){
                $this->array2xml($value, $xml->addChild($key));
            }else{
                $xml->addChild($key, $value);
            }
        }
        return $xml->asXML();
    }
    
}